#pragma once
#include<iostream>
using namespace std;
template <class K>
struct BStreenode
{
	BStreenode<K>* _left;
	BStreenode<K>* _right;
	K _key;
	BStreenode(const K& key)
		:_left(nullptr)
		, _right(nullptr)
		, _key(key)
	{}
};
template <class K>
class BStree
{ 
	typedef BStreenode<K> Node;
public:

	


	bool insert(const K& key)
	{
		if (_root == nullptr)
		{
			_root = new Node(key);
			return _root;
		}
		Node* parent =nullptr;
		Node* cur = _root;
		while (cur)
		{
			if (cur->_key < key)
			{
				parent = cur;
				cur = cur->_right;
			}
			else if (cur->_key > key)
			{
				parent = cur;
               cur = cur->_left;

			}
			else
			{
				return false;

			}






		}
		cur= new Node(key);
		if (cur->_key >parent->_key)
		{
			parent->_right = cur;

		}
		else
		{
			parent->_left = cur;

		}


	
		return true;
	
	}
	void inorder()
	{
		_inorder(_root);
	
	}
	bool find(const K& key)
	{
		if (_root == nullptr)
		{
			return false;
		}
		Node* cur = _root;
		while (cur)
		{
		
			if (cur->_key > key)
			{
				cur = cur->_left;


			}
			else if (cur->_key < key)
			{
				cur = cur->_right;
			}
			else
			{

				return true;
			}
		}
		return false;
	}
	bool earse(const K& key)
	{
		if (_root == nullptr)
			return false;
		Node* cur = _root;
		Node* parent = cur;

		while (cur)
		{
			if (cur->_key < key)
			{
				parent = cur;
				cur = cur->_right;


			}
			else if (cur->_key > key)
			{
				parent = cur;
				cur = cur->_left;

			}
			else
			{


				if (cur->_left == nullptr)
				{
					if (cur == _root)
					{
						_root = cur->_right;


					}
					else
					{
						if (parent->_left == cur)
						{

							parent->_left = cur->_right;

						}
						else if (parent->_right == cur)
						{
							parent->_right = cur->_right;
						}
					}

				
					delete cur;


				}
				else if (cur->_right == nullptr)
				{
					if (cur == _root)
					{
						_root = cur->_left;


					}
					else
					{
						if (parent->_left == cur)
						{

							parent->_left = cur->_left;

						}
						else if (parent->_right == cur)
						{
							parent->_right = cur->_left;
						}
					}
					delete cur;

				}
				else
				{
					Node* prev = cur;
					Node* per = cur->_right;
					while (per->_left)
					{   prev = per;
						per = per->_left;
					}
					cur->_key = per->_key;
					
					if (per == prev->_left)
					{
                       prev->_left = per->_right;
					}
					if (per == prev->_right)
					{
						prev->_right = per->_right;
					}
					delete per;
					







				}






				return true;

			}




		}


	}


private:

	Node* _root = nullptr;
	void _inorder(Node* _root)
	{
		if (_root == nullptr)
		{
			return;
		}
		_inorder(_root->_left);
		cout << _root->_key << " ";
		_inorder(_root->_right);
	}
};
void test1()
{   BStree<int>st;
	int a[] = { 8,3,1,10,6,4,7,14,13 };
	for (auto e : a)
	{
		st.insert(e);
	}

	st.earse(8);
	st.inorder();
	

}